package com.yss.kettle.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobDuties;
import com.yss.kettle.service.IJobDutiesService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 采集任务信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/duties")
public class JobDutiesController extends BaseController {
    @Autowired
    private IJobDutiesService jobDutiesService;

    /**
     * 查询采集任务信息列表
     */
    @RequiresPermissions("kettle:duties:list")
    @GetMapping("/list")
    public TableDataInfo list(JobDuties jobDuties) {
        startPage();
        List<JobDuties> list = jobDutiesService.selectJobDutiesList(jobDuties);
        return getDataTable(list);
    }

    /**
     * 导出采集任务信息列表
     */
    @RequiresPermissions("kettle:duties:export")
    @Log(title = "采集任务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobDuties jobDuties) {
        List<JobDuties> list = jobDutiesService.selectJobDutiesList(jobDuties);
        EasyExeclUtil.export(response, list, JobDuties.class);
    }

    /**
     * 获取采集任务信息详细信息
     */
    @RequiresPermissions("kettle:duties:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(jobDutiesService.getById(id));
    }

    /**
     * 新增采集任务信息
     */
    @RequiresPermissions("kettle:duties:add")
    @Log(title = "采集任务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobDuties jobDuties) {
        jobDuties.setId(IdUtils.fastSimpleUUID());
        jobDuties.setCreateBy(SecurityUtils.getUsername());
        jobDuties.setCreateTime(new Date());
        return toAjax(jobDutiesService.save(jobDuties));
    }

    /**
     * 修改采集任务信息
     */
    @RequiresPermissions("kettle:duties:edit")
    @Log(title = "采集任务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobDuties jobDuties) {
        jobDuties.setUpdateTime(new Date());
        jobDuties.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jobDutiesService.updateById(jobDuties));
    }

    /**
     * 删除采集任务信息
     */
    @RequiresPermissions("kettle:duties:remove")
    @Log(title = "采集任务信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobDutiesService.removeByIds(ids));
    }
}
